package com.nd.bdc.presto.util;

import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

/**
 * @ClassName : ResourceFileLocator  //类名
 * @Description : jar资源文件拷贝工具类  //描述
 * @Author : ggh  //作者
 * @Date: 2020-05-07 14:03  //时间
 */
@Component
public class ResourceFileLocator {

    /* 把文件从自带的jar包中拷贝出来，移入指定文件夹 */
    public static void copyFile(String path, File dest) {
        String resourceName = path;
        try {
            if (!copy(ResourceFileLocator.class.getResourceAsStream(resourceName), dest.getAbsolutePath())) {
                throw new NullPointerException();
            }
        } catch (NullPointerException ex) {
            throw ex;
        }
    }

    // 以文件的形式把流存入指定文件夹内
    private static boolean copy(InputStream source, String destination) {
        boolean success = true;
        try {
            Files.copy(source, Paths.get(destination), StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException ex) {
            success = false;
        }
        return success;
    }
}
